//
//  SSCommentExtension.swift
//  SSCommentUI
//
//  Created by 松松 on 2019/5/24.
//

import Foundation
import SSComment

extension UIView{

    class func fromNib<T>(_ aClass: T.Type) -> T {
        let name = String(describing: aClass)
        if Bundle.getCurrentFramwork().path(forResource: name, ofType: "nib") != nil {
            return UINib(nibName: name, bundle:Bundle.getCurrentFramwork()).instantiate(withOwner: nil, options: nil)[0] as! T
        } else {
            fatalError("\(String(describing: aClass)) nib is not exist")
        }
    }

}

extension Bundle{

    class func getCurrentFramwork() -> Bundle{
        var currentBundlePath = Bundle.main.path(forResource: "Frameworks", ofType: nil)
        currentBundlePath = "\(currentBundlePath!)/SSCommentUI.framework"
        //找到当前pod库的bundle
        let bundle :Bundle = Bundle(path: currentBundlePath!)!
        return bundle
    }

}

extension UIViewController{


    public func showSSComment(_ customUserModel: CommentUserModel, loginBlock: CommentLoginBlock?){

        let extensions = customUserModel.mainTypeColor
        let json = extensions?.data(using: .utf8)
        let colorDic = try! JSONSerialization.jsonObject(with: json!, options: .allowFragments)
        let colorModel = SSCommentColorModel(fromDictionary: colorDic as! [String:Any])
        userModel = customUserModel
        commentLoginBlock = loginBlock
        if colorModel.pop{
            let vc = SSCommentPopViewController()
            vc.colorModel = colorModel
            let nav = UINavigationController(rootViewController: vc)
            nav.setNavigationBarHidden(true, animated: false)
            nav.modalPresentationStyle = .overFullScreen
            nav.definesPresentationContext = true
            nav.view.backgroundColor = UIColor.clear

            self.present(nav, animated: true, completion: nil)
        }else{
            let vc = SSCommentViewController()
            vc.colorModel = colorModel
            let nav = UINavigationController(rootViewController: vc)
            let animat = CATransition()
            animat.duration = 0.3
            animat.type = kCATransitionMoveIn
            animat.subtype = kCATransitionFromRight
            UIApplication.shared.keyWindow?.layer.add(animat, forKey: nil)
            self.present(nav, animated: false, completion: nil)
        }
    }

}


